<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the searchForSentences method from the MassifExampleSentencesEnhancement class, for the Dart programming language.">
  <title>searchForSentences method - MassifExampleSentencesEnhancement class - creator library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false" class="light-theme">

<div id="overlay-under-drawer"></div>

<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">yuuna</a></li>
    <li><a href="../../creator/creator-library.html">creator</a></li>
    <li><a href="../../creator/MassifExampleSentencesEnhancement-class.html">MassifExampleSentencesEnhancement</a></li>
    <li class="self-crumb">searchForSentences method</li>
  </ol>
  <div class="self-name">searchForSentences</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        brightness_4
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        brightness_5
      </span>
    </label>
  </div>
</header>
<main>

  <div id="dartdoc-main-content" class="main-content">
      <div>
<h1><span class="kind-method">searchForSentences</span> method 
</h1></div>

    <section class="multi-line-signature">
      

<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="https://api.flutter.dev/flutter/dart-core/List-class.html">List</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="../../creator/MassifResult-class.html">MassifResult</a></span>&gt;</span></span>&gt;</span></span>
<span class="name ">searchForSentences</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="searchForSentences-param-context">{<span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/widgets/BuildContext-class.html">BuildContext</a></span> <span class="parameter-name">context</span>, </span></li>
<li><span class="parameter" id="searchForSentences-param-appModel"><span>required</span> <span class="type-annotation"><a href="../../models/AppModel-class.html">AppModel</a></span> <span class="parameter-name">appModel</span>, </span></li>
<li><span class="parameter" id="searchForSentences-param-searchTerm"><span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a></span> <span class="parameter-name">searchTerm</span>}</span></li>
</ol>)

      

    </section>
    
<section class="desc markdown">
  <p>Search the Massif API for example sentences and return a list of results.</p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">Future&lt;List&lt;MassifResult&gt;&gt; searchForSentences({
  required BuildContext context,
  required AppModel appModel,
  required String searchTerm,
}) async {
  if (searchTerm.trim().isEmpty) {
    return [];
  }

  if (_massifCache[searchTerm] != null) {
    return _massifCache[searchTerm]!;
  }

  List&lt;MassifResult&gt; results = [];

  late http.Response response;

  try {
    &#47;&#47;&#47; Query the Massif API for results.
    response = await _client.get(Uri.parse(
        &#39;https:&#47;&#47;massif.la&#47;ja&#47;search?&amp;fmt=json&amp;q=${Uri.encodeComponent(searchTerm)}&#39;));

    Map&lt;String, dynamic&gt; json = jsonDecode(utf8.decode(response.bodyBytes));

    &#47;&#47;&#47; For each response, create a [MassifResult] that can be used to display
    &#47;&#47;&#47; the widget as well as hold the sentence and source data.
    List&lt;Map&lt;String, dynamic&gt;&gt; queryResponses =
        List&lt;Map&lt;String, dynamic&gt;&gt;.from(json[&#39;results&#39;]);

    for (Map&lt;String, dynamic&gt; queryResponse in queryResponses) {
      Map&lt;String, String&gt; sampleSource =
          Map&lt;String, String&gt;.from(queryResponse[&#39;sample_source&#39;]);
      String source = sampleSource[&#39;title&#39;]!;
      String text = queryResponse[&#39;text&#39;];

      List&lt;InlineSpan&gt; spans = [];

      String highlightedText = queryResponse[&#39;highlighted_html&#39;];
      List&lt;String&gt; splitWithDelims =
          highlightedText.splitWithDelim(RegExp(r&#39;&lt;em&gt;(.*?)&lt;\&#47;em&gt;&#39;));

      final buffer = StringBuffer();
      TextRange range = TextRange.empty;

      bool firstFound = false;
      bool consecutiveFlag = false;
      int? start;
      int? end;

      for (String splitWithDelim in splitWithDelims) {
        if (splitWithDelim.startsWith(&#39;&lt;em&gt;&#39;) &amp;&amp;
            splitWithDelim.endsWith(&#39;&lt;&#47;em&gt;&#39;)) {
          String text =
              splitWithDelim.replaceAll(&#39;&lt;em&gt;&#39;, &#39;&#39;).replaceAll(&#39;&lt;&#47;em&gt;&#39;, &#39;&#39;);

          if (!firstFound) {
            consecutiveFlag = true;
            firstFound = true;
            start = buffer.length;
            end = buffer.length + text.length;
          }

          if (firstFound &amp;&amp; consecutiveFlag) {
            end = buffer.length + text.length;
          }

          buffer.write(text);

          spans.add(
            TextSpan(
              text: text,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Theme.of(context).colorScheme.primary,
                fontSize: Theme.of(context).textTheme.titleMedium?.fontSize,
              ),
            ),
          );
        } else {
          if (splitWithDelim.trim().isNotEmpty) {
            consecutiveFlag = false;
          }

          buffer.write(splitWithDelim);
          spans.add(
            TextSpan(
              text: splitWithDelim,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: Theme.of(context).textTheme.titleMedium?.fontSize,
              ),
            ),
          );
        }
      }

      if (start != null &amp;&amp; end != null) {
        range = TextRange(
          start: start,
          end: end,
        );
      }

      MassifResult result = MassifResult(
        text: text,
        range: range,
        source: source,
        spans: spans,
      );

      results.add(result);
    }

    &#47;&#47;&#47; Save this into cache.
    _massifCache[searchTerm] = results;

    return results;
  } catch (e) {
    &#47;&#47;&#47; Used to log if this third-party service is down or changes domains.
    appModel.showFailedToCommunicateMessage();
    throw Exception(
      &#39;Failed to communicate with Massif: ${response.reasonPhrase}&#39;,
    );
  }
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
  <li><a href="../../index.html">yuuna</a></li>
  <li><a href="../../creator/creator-library.html">creator</a></li>
  <li><a href="../../creator/MassifExampleSentencesEnhancement-class.html">MassifExampleSentencesEnhancement</a></li>
  <li class="self-crumb">searchForSentences method</li>
</ol>


    <h5>MassifExampleSentencesEnhancement class</h5>
    <ol>

        <li class="section-title"><a href="../../creator/MassifExampleSentencesEnhancement-class.html#constructors">Constructors</a></li>
          <li><a href="../../creator/MassifExampleSentencesEnhancement/MassifExampleSentencesEnhancement.html">MassifExampleSentencesEnhancement</a></li>


        <li class="section-title inherited">
          <a href="../../creator/MassifExampleSentencesEnhancement-class.html#instance-properties">Properties</a>
        </li>
          <li class="inherited"><a href="../../creator/Enhancement/description.html">description</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/descriptionLocalisation.html">descriptionLocalisation</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/field.html">field</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/hashCode.html">hashCode</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/icon.html">icon</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/label.html">label</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/labelLocalisation.html">labelLocalisation</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/runtimeType.html">runtimeType</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/uniqueKey.html">uniqueKey</a></li>

        <li class="section-title"><a href="../../creator/MassifExampleSentencesEnhancement-class.html#instance-methods">Methods</a></li>
          <li><a href="../../creator/MassifExampleSentencesEnhancement/enhanceCreatorParams.html">enhanceCreatorParams</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/getLocalisedDescription.html">getLocalisedDescription</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/getLocalisedLabel.html">getLocalisedLabel</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/initialise.html">initialise</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/noSuchMethod.html">noSuchMethod</a></li>
          <li class="inherited"><a href="../../creator/Enhancement/prepareResources.html">prepareResources</a></li>
          <li><a href="../../creator/MassifExampleSentencesEnhancement/searchForSentences.html">searchForSentences</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/toString.html">toString</a></li>

        <li class="section-title inherited"><a href="../../creator/MassifExampleSentencesEnhancement-class.html#operators">Operators</a></li>
          <li class="inherited"><a href="https://api.flutter.dev/flutter/dart-core/Object/operator_equals.html">operator ==</a></li>




        <li class="section-title"><a href="../../creator/MassifExampleSentencesEnhancement-class.html#constants">Constants</a></li>
          <li><a href="../../creator/MassifExampleSentencesEnhancement/key-constant.html">key</a></li>
</ol>

  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    yuuna
      2.8.0+93
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

